var apps = getApp();
var village_LBS=function(that){
  // ------------ 腾讯LBS地图  --------------------
  wx.getLocation({
    type: 'gcj02', //返回可以用于wx.openLocation的经纬度
    success: function (res) {
      that.setData({
        latitude: res.latitude,
        longitude: res.longitude
      })
    }
  });
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude:'',
    longitude: '', 
    markers:[
    ],
    userInfo: {},
    hasUserInfo: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //判断全局userinfo 变量里面是否有值
    if (!apps.globalData.userInfo) {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          apps.globalData.userInfo = res.userInfo
        }
      })
    } 
  
    //获取用户的位置
    var that = this;
    village_LBS(that);
    wx.login({
      success: function (res) {
        //登录成功
        if (res.code) {
          wx.request({
            url: 'https://yangyh.applinzi.com/address.php',
            data: {
              latitude: that.data.latitude,
              longitude:that.data.longitude,
              code: res.code,
              nickname: apps.globalData.userInfo.nickName
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            dataType: "json",
            method: "post",
            success: function (rs) {
              //全局变量openID赋值
              apps.globalData.openId = rs.data.open_id
              var laox_arr = rs.data.info;
              var marks = [];
              //请求的数据不为空
              if(laox_arr !=''){               
                for (var j = 0; j < laox_arr.length; j++) {
                  var mrk = {
                    iconPath: "../image/location2.png",
                    id: laox_arr[j].id,
                    latitude: laox_arr[j].latitude,
                    longitude: laox_arr[j].longitude,
                    width: 50,
                    height: 50,
                    // label: {
                    //   x: -24,
                    //   y: -26,
                    //   content: laox_arr[j].nickname
                    // },
                  }
                  marks.push(mrk);
                }
              }
              that.setData({
                markers: marks
              })
            }
          })
        }
      }
    }),
    wx.getSystemInfo({
      success: function (res) {
        //设置map高度，根据当前设备宽高满屏显示
        that.setData({
          view: {
            Height: res.windowHeight
          },
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that=this;
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
            success: function (res) {
              if (res.cancel) {
                console.info("1授权失败返回数据");

              } else if (res.confirm) {
                village_LBS(that);
                wx.openSetting({
                  success: function (data) {
              
                    if (data.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 5000
                      })
                      //再次授权，调用getLocationt的API
                      village_LBS(that);
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 5000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
          village_LBS(that);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onshow xianshi")
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '找老乡',
      path: '/page/location/location',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  
})